<?php
/**
 * 1.动态列表
 */
namespace app\trends\controller;
use app\common\controller\HomeCommon;
use think\Request;

class Index extends HomeCommon
{
	/**
	 * 1、大厅动态列表
	 * @return \think\response\Json
	 */
	
	
    public function index()
    {

    	if(Request::instance()->isPost()){
    		
    		$where="1=1 and a.status=1 ";
    		if(input('token')){
    			$token=input('token');
    			//验证token
    			$result=$this->check_token($token);
    			
    			if($result['errcode']!=0){
    				
    				return json(['errcode'=>'10001','msg'=>'token错误！','data'=>[]]);die;
    			}
    			$where.=" and a.user_id=".$this->user_id;
    			//获得当前用户加入的园区
    			$park_list=db('users_parks')->field('parkid')->where(['status'=>1,'user_id'=>$this->user_id])->select();
    			if(!empty($park_list)){
    				$park_str="";
    				$park_arr=[];
    				foreach ($park_list as $val){
    					$park_arr[]=$val['parkid'];
    				}
    				$park_str=implode(',',$park_arr);
    				$where.=" and concat(',',a.park_id,',') regexp concat(',(',replace('$park_str',',','|'),'),') ";
    			}
    		} 
    		
    		
    		
    		$list=db('trends')
    		->alias('a')
    		->field('a.id trends_id,a.title,a.desc,a.images as background_img,a.create_time,a.like_num,b.images headimg,b.username')
    		->join('users b','a.user_id=b.id')
    		->where($where)
    		->order('a.id desc')	
    		->paginate(15);
    		$list=$list->toArray();
    		
    		
    		foreach ($list['data'] as $key=>$val){
    			$img_arr=explode(',',$val['background_img']);
    			$background=[];
    			if($img_arr){
    				
    				foreach ($img_arr as $key1=>$val){
    					$image = getImageinfo($val);
    					$background[$key1]=$image;
    					$background[$key1]['url']=$val;
    					$background[$key1]['url']=str_replace("\\","/",$val);
    				}
    				
    			}
    			/* $image = \think\Image::open('./addWater.png'); //要加水印的图片
    			
    			// 返回图片的宽度
    			$width = $image->width();
    			// 返回图片的高度
    			$height = $image->height(); */
    			//$list['data'][$key]['background']=[];
    			
    			$list['data'][$key]['background']=$background;
    		}
    		
    		return json(['errcode'=>'0','msg'=>'获取成功！','data'=>$list]);
    	}
    	
    }
}
